//
//  DataConverter.m
//  Lebo
//
//  Created by Gary on 15/7/1
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "DataConverter.h"

#import "BrandModel.h"
#import "BrandSetModel.h"

#define DEFINE_TEMP_VARIABLE id value = nil

#define SET_TEMP_VARIABLE_BY_DIC_VALUE(dic, keyInDic) value = [dic objectForKey:keyInDic]; \
                                \
if (value == [NSNull null]) {   \
    value = @"";                \
}                               \
if(value == nil) {              \
    value = @"";                \
}                               \

#define SET_PROPERTY_INT_BY_DIC_KEY(propertyInt, dic, keyInDic) SET_TEMP_VARIABLE_BY_DIC_VALUE(dic, keyInDic); \
if (value) {\
propertyInt = [value integerValue]; \
}else {                             \
  propertyInt = 0;                        \
}


#define SET_PROPERTY_LL_BY_DIC_KEY(propertyll, dic, keyInDic) SET_TEMP_VARIABLE_BY_DIC_VALUE(dic, keyInDic); \
if (value) {\
propertyll = [value longLongValue]; \
}else {                             \
  propertyll = 0;                         \
}


#define SET_PROPERTY_STR_BY_DIC_KEY(propertyStr, dic, keyInDic) SET_TEMP_VARIABLE_BY_DIC_VALUE(dic, keyInDic); \
if (value != nil || ![value isEqual:[NSNull null]] ) {\
propertyStr = value; \
}else{               \
   propertyStr = @"";  \
}


#define SET_PROPERTY_BOOL_BY_DIC_KEY(propertyInt, dic, keyInDic) SET_TEMP_VARIABLE_BY_DIC_VALUE(dic, keyInDic); \
if (value) {\
if ([value boolValue]) { \
propertyInt = 1; \
} else { \
propertyInt = 0;\
}\
}

@implementation DataConverter

/**
 * @brief 将品牌的数据字典转换成模型
 *
 * @param [in]
 * @param [out]
 * @return
 * @note
 */
+ (id)ConvertBrandDicToModel:(NSDictionary*)dic {
    if (!dic) {
        return nil;
    }
 
    BrandModel *brand = [[BrandModel alloc] init];
    DEFINE_TEMP_VARIABLE;
    
    SET_PROPERTY_STR_BY_DIC_KEY(brand.extCode, dic, @"extcode");
    SET_PROPERTY_STR_BY_DIC_KEY(brand.letter, dic, @"letter");
    SET_PROPERTY_STR_BY_DIC_KEY(brand.name, dic, @"name");
    SET_PROPERTY_STR_BY_DIC_KEY(brand.logo , dic , @"logofilename");
    
    return brand;
}

/**
 * @brief 将任务模型转换成数据字典
 *
 * @param [in]
 * @param [out]
 * @return
 * @note
 */
+ (id)ConvertBrandSetDicToModel:(NSDictionary*)dic {
    if (!dic) {
        return nil;
    }
    
    BrandSetModel *brandSet = [[BrandSetModel alloc] init];
    DEFINE_TEMP_VARIABLE;
    
    SET_PROPERTY_STR_BY_DIC_KEY(brandSet.extCode, dic, @"extcode");
    SET_PROPERTY_STR_BY_DIC_KEY(brandSet.modelName, dic, @"modelname");
    SET_PROPERTY_STR_BY_DIC_KEY(brandSet.brandName, dic, @"brandname");
    SET_PROPERTY_STR_BY_DIC_KEY(brandSet.brandId , dic , @"brandid");
    
    return brandSet;
}

@end
